package com.guanghua.brick.html;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guanghua.brick.html.tag.WebAppContextTag;

public class DateTimePickerControl extends WebControl {
	
	private String format = "%y-%M-%d";
	private boolean readOnly = true;
	
	@Override
	public String buildHTML(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer html = new StringBuffer();
		
		html.append("<input id=\"")
			.append(this.getId())
			.append("\" type=\"text\" name=\"")
			.append(this.getName())
			.append("\"");
		
		if (this.getData() != null)
			html.append(" value=\"").append(this.getData()).append("\"");
		
		html.append(" format=\"")
			.append(this.format)
			.append("\" ")
			.append((this.readOnly)?"readonly":"")
			.append(" ")
			.append(this.getHtml())
			.append("/>\n<img id=\"")
			.append(this.getId())
			.append(".button\" src=\"")
			.append(WebAppContextTag.getWebAppContext(request))
			.append("/brick/image/datetime.gif\" class=\"img_button\"/>\n");
		
		html.append("<div id=\"")
			.append(this.getId())
			.append(".div\" class=\"datetime_div\">\n<iframe style=\"position:absolute;z-index:-1;width:100%;height:100%;top:0;left:0;scrolling:no;\" frameborder=\"0\" src=\"about:blank\"></iframe>\n</div>\n");
		
		html.append("<script language=\"javascript\">\n");
		html.append("	var ")
		.append(this.getId())
		.append(" = new DateTime(\"")
		.append(this.getId())
		.append("\");\n");
		html.append("</script>\n");
		return html.toString();
	}
	
	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public boolean isReadOnly() {
		return readOnly;
	}

	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}
}
